package com.csc.dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Date;

import com.csc.data.BookObj;

public class BookDAOImpl implements BookDAO{
	public static final String DRIVER=
	    "sun.jdbc.odbc.JdbcOdbcDriver";
	public static final String DBURL=
		"jdbc:odbc:BookDB";
	
	private Connection getConnection(){
		Connection con = null;
		try {
			Class.forName(DRIVER);
			con = DriverManager.getConnection(DBURL, "", "");
		} catch (Exception e) {
			e.printStackTrace();
		}
		return con;
	}
	
	public boolean deleteBook() {
		return false;
	}

	public BookObj findBook(int bookId) {
		// TODO Auto-generated method stub
		BookObj bookObj = new BookObj();
		try {
			Connection con = getConnection();
			Statement st = con.createStatement();
			String query = "select * from Books where ID=" + bookId;
			ResultSet result = st.executeQuery(query);
			
			if(result.next()){
				bookObj.setBookId(Integer.parseInt(result.getString("ID")));
				bookObj.setAuthor(result.getString("Author"));
				bookObj.setIsbn(result.getString("ISBN"));
				bookObj.setPrice(result.getString("Price"));
				bookObj.setTitle(result.getString("Title"));
			}
			else{
				bookObj = null;
			}
			
			con.close();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return bookObj;
	}

	public int insertBook(int bookId, String title, String author, String ISBN, String price, Date publishDate) {
		// TODO Auto-generated method stub
		return 0;
	}

	public ArrayList<BookObj> selectBooks() {
		 
		ArrayList<BookObj> arr = new ArrayList<BookObj>();
		try {
			Connection con = getConnection();
			Statement st = con.createStatement();
			String query = "select * from Books";
			ResultSet result = st.executeQuery(query);
			while(result.next()){
				BookObj book = new BookObj();
				book.setBookId(Integer.parseInt(result.getString("ID")));
				book.setAuthor(result.getString("Author"));
				book.setIsbn(result.getString("ISBN"));
				book.setPrice(result.getString("Price"));
				book.setTitle(result.getString("Title"));
				arr.add(book);
			}
			con.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return arr;
	}

	public boolean updateBook(BookObj bookObj) {
		// TODO Auto-generated method stub
		try {
			int id = bookObj.getBookId();
			String title = bookObj.getTitle();
			String author = bookObj.getAuthor();
			String isbn = bookObj.getIsbn();
			String price = bookObj.getPrice();
			
			Connection con = getConnection();
			Statement st = con.createStatement();
			String query = "update Books " +
						   "set Title='" + title + "',Author='" + author + "',ISBN='" + isbn + "',Price='" + price + "'" +
						   "where ID=" + id;
//			System.out.println(query);
			
			if(st.executeUpdate(query) != 0){
				con.close();
				return true;
			}
			else{
				con.close();
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return false;
	}
	

}
